﻿using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
using BleQua.Data;

namespace BleQua {
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application {
		public static ManagerCenter DataSource {
			get {
				if (App.Current.Resources.Contains("DataSource")) {
					var resource = App.Current.Resources["DataSource"];
					if (resource is ObjectDataProvider) {
						var objectDataProvider = (ObjectDataProvider)resource;
						if (objectDataProvider.ObjectType == typeof(ManagerCenter))
							return (ManagerCenter)objectDataProvider.ObjectInstance;
					} else if (resource is ManagerCenter) {
						return (ManagerCenter)resource;
					}
				}
				return null;
			}
		}

		//public static Core.Main Core = new Core.Main( ) { Config = new Core.Basic.Config( ) { StartupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly( ).Location) } };

		//public App( ) {
		//App.Current.Resources["MainManagerCemter"] = new ManagerCenter( );
		//App.Current.Startup += new StartupEventHandler(Current_Startup);
		//App.Current.Exit += new ExitEventHandler(Current_Exit);
		//}

		//[PreEmptive.Attributes.Setup(CustomEndpoint = "so-s.info/PreEmptive.Web.Services.Messaging/MessagingServiceV2.asmx")]
		//private void Current_Startup(object sender, StartupEventArgs e) {
		//Uri uri = new Uri("/ModernTheme;component/ModernTheme.xaml", UriKind.RelativeOrAbsolute);
		//ResourceDictionary resourceDictionary = Application.LoadComponent(uri) as ResourceDictionary;
		//this.Resources.MergedDictionaries.Add(resourceDictionary);

		//Core = new Core.Main( ) { Config = new Core.Basic.Config( ) { StartupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly( ).Location) } };
		///Core.Reload( );

		//this.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);

		//this.MainWindow = new MainWindow();
		//this.MainWindow.Resources = this.Resources;
		//}

		[PreEmptive.Attributes.TeardownAttribute( )]
		private void Current_Exit(object sender, ExitEventArgs e) {
			//Core.Dispose( );
		}

		private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
#if !DEBUG
			Alfa.Common.UI.Dialogs.ErrorMessageWindow errorWindow = new Alfa.Common.UI.Dialogs.ErrorMessageWindow(ex, "http://apps.abstractsoft.eu/BleQua/BugTracer");
			errorWindow.ShowDialog( );
#endif
		}
	}
}
